package com.yourenbang.lingyun.transaction.web.service.impl.pay.transaction;

import com.alibaba.fastjson.JSONObject;
import com.yourenbang.lingyun.constants.enums.pay.PayTypeEnum;
import com.yourenbang.lingyun.constants.enums.table.PayTransactionEnum;
import com.yourenbang.lingyun.core.util.KLog;
import com.yourenbang.lingyun.core.util.StringUtils;
import com.yourenbang.lingyun.core.util.date.DateFormat;
import com.yourenbang.lingyun.domain.Merchant21;
import com.yourenbang.lingyun.transaction.constant.LklTransTypeEnum;
import com.yourenbang.lingyun.transaction.web.service.PayTransactionFormatService;
import com.yourenbang.merchant.api.service.yxy.MerchantYxyApiService;
import com.yourenbang.transaction.api.model.vo.pay.PayTransaction;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Map;
import java.util.Objects;

@Service
@Slf4j
public class WznPayTransactionFormatServiceImpl implements PayTransactionFormatService {
    @Autowired
    MerchantYxyApiService merchantYxyApiService;

    @Override
    public PayTransaction payTransactionFormat(JSONObject dto) {
        Merchant21 merchant21 = merchantYxyApiService.getMerchant21ByMerchantNo(dto.getString("mercId"));
        if (Objects.isNull(merchant21)) {
            KLog.info("微智能交易回调获取商户为空");
            return null;
        }
        PayTransaction payTransaction = new PayTransaction();
        payTransaction.setUserBaseId(merchant21.getUserBaseId());
        payTransaction.setMerchantId(merchant21.getMerchantId());
        payTransaction.setMerchantNo(merchant21.getMerchantNo());
        payTransaction.setMerchantName(merchant21.getMerchantName());
        payTransaction.setPhone(merchant21.getPhone());
        payTransaction.setRealName(merchant21.getRealName());
        payTransaction.setOrderNo(dto.getString("logNo"));
        payTransaction.setTransactionNo(dto.getString("ordNo"));
        payTransaction.setTransactionAmount(dto.getBigDecimal("txnAmt"));
        payTransaction.setRate(dto.getBigDecimal("mercFee").divide(dto.getBigDecimal("txnAmt"), 4, RoundingMode.HALF_UP)
                .multiply(new BigDecimal("10000")).intValue());
        payTransaction.setTransactionFee(dto.getBigDecimal("mercFee"));
        payTransaction.setCardFlag(formatCardFlag(dto.getString("crdFlg"), dto.getString("rateCd")));
        payTransaction.setSn(dto.getString("snNo"));
        payTransaction.setTransactionCardNo(dto.getString("crdNo"));
        payTransaction.setTransactionCardType(payTransaction.getCardFlag());
        payTransaction.setStatus(PayTransactionEnum.TransactionStatus.STATUS_10.getCode());
        payTransaction.setTransactionTime(DateFormat.DATETIME_COMPACT.parse(dto.getString("txnTm")));
        payTransaction.setTransactionMonth(StringUtils.left(dto.getString("txnTm"), 6));
        payTransaction.setTransactionDay(StringUtils.left(dto.getString("txnTm"), 8));
        payTransaction.setFrontSuccessTime(payTransaction.getTransactionTime());
        payTransaction.setFeeAmt(BigDecimal.ZERO);
        payTransaction.setAccAmt(dto.getBigDecimal("txnAmt").subtract(dto.getBigDecimal("mercFee")));
        payTransaction.setChannelId(merchant21.getChannelId());
        payTransaction.setPlatNo(merchant21.getPlatNo());
        payTransaction.setIsDel(false);
        payTransaction.setTransType(StringUtils.equals(dto.getString("actTxnFlg"), "1") ? LklTransTypeEnum.TYPE_412001.getTransType() :
                LklTransTypeEnum.TYPE_012002.getTransType());
        payTransaction.setProfitFlag(checkIsProfitFlag(payTransaction));
        return payTransaction;
    }

    @Override
    public JSONObject dyUrlRequest(String uri, Map<String, Object> bodyJson) {
        return null;
    }

    @Override
    public JSONObject dyUrlRequestByPayType(String uri, Map<String, Object> bodyJson, PayTypeEnum payTypeEnum) {
        return null;
    }

    /**
     * 格式化卡类型
     *
     * @param crdFlg 卡类型
     * @param rateCd 费率类型
     * @return 银小云卡类型
     */
    private String formatCardFlag(String crdFlg, String rateCd) {
        if ("06".equals(crdFlg)) {
            return PayTransactionEnum.CardFlagEnum.CREDIT_CARD.getCode();
        } else if ("05".equals(crdFlg)) {
            return PayTransactionEnum.CardFlagEnum.BORROW_CARD.getCode();
        } else if ("WX".equals(rateCd)) {
            return PayTransactionEnum.CardFlagEnum.WECHAT.getCode();
        } else if ("ALI".equals(rateCd)) {
            return PayTransactionEnum.CardFlagEnum.ALIPAY.getCode();
        } else if ("UN".equals(rateCd)) {
            return PayTransactionEnum.CardFlagEnum.UNIONCODE.getCode();
        } else {
            return crdFlg + "_" + rateCd;
        }
    }

    //判断是否计入分润
    private boolean checkIsProfitFlag(PayTransaction payTransaction) {
        //贷记卡且非押金交易才计入分润
        if (StringUtils.equalsAny(payTransaction.getCardFlag(), PayTransactionEnum.CardFlagEnum.ALIPAY.getCode(),
                PayTransactionEnum.CardFlagEnum.WECHAT.getCode(),
                PayTransactionEnum.CardFlagEnum.CREDIT_CARD.getCode(), PayTransactionEnum.CardFlagEnum.UNIONCODE.getCode()) &&
                !StringUtils.equals(LklTransTypeEnum.TYPE_412001.getTransType(), payTransaction.getTransType())) {
            return true;
        }
        return false;
    }
}
